package com.topscomm.file.service;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.topscomm.basic.exception.ServiceException;
import com.topscomm.file.util.FileUtil;
import com.topscomm.file.util.YamUtil;
import com.topscomm.meet.MeetSystemConst.FileUploadStateEnum;
import com.topscomm.meet.service.IFileManageService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;

import lombok.extern.slf4j.Slf4j;

/**
 * 文件加密服务链
 * 
 * @description:
 * @author: donghang
 * @date: 2020年8月20日下午12:55:09
 * @modify:
 */
@Slf4j
public class FilePasswordFilter implements IFileProcessFilter {
	/**
	 * 根路径
	 */
	private String rootPath;

	/**
	 * 
	 * @description:
	 * @author: donghang
	 * @date: 2020年8月21日上午11:34:03
	 * @modify:
	 */
	public FilePasswordFilter() {
		this.rootPath = YamUtil.getVal("file.audio.root.path");
	}

	/**
	 * 文件加密服务处理
	 * 
	 * @description:
	 * @param paraMap
	 * @param dataProcessChain
	 * @return
	 * @author: donghang
	 * @date: 2020年8月20日下午1:43:00
	 * @modify:
	 */
	@Override
	public boolean doProcess(Map<String, Object> paraMap, IFileProcessChain dataProcessChain) {
		String fileName = ConvertUtil.convertToString(paraMap.get("fileName"));
		String identifier = ConvertUtil.convertToString(paraMap.get("identifier"));
		String sourceid = ConvertUtil.convertToString(paraMap.get("sourceid"));
		String sourcetype = ConvertUtil.convertToString(paraMap.get("sourcetype"));
		String location = ConvertUtil.convertToString(paraMap.get("foldername"));
		StringBuilder filePath = new StringBuilder();
		// 合并后的文件
		String inputFilePath = filePath.append(this.rootPath).append("/").append(location).append("/").append(fileName)
				.toString();
		IFileManageService fileManageService = (IFileManageService) SpringUtil.getBean("fileManageService");
		// 判断是否存在原文件，如果不存在则认为已经合并，继续链处理
		File inputFile = new File(inputFilePath);
		if (!inputFile.exists()) {
			log.info("文件 ：" + inputFilePath + "先前已经加密完成");
			Map<String, Object> updataMap = new HashMap<String, Object>();
			updataMap.put("uploadstate", FileUploadStateEnum.Splice);
			StringBufferProxy sql = new StringBufferProxy();
			sql.appendSingle(" 1=1 ");
			sql.appendSingle(" and identifier = '{0}' and sourceid='{1}' and sourcetype='{2}'", identifier, sourceid,
					sourcetype);
			fileManageService.updateByWhere(updataMap, sql.toString());
			dataProcessChain.doProcess(paraMap);
			return true;
		}
		// 得到文件上级目录
		try {
			// 文件加密处理
			FileUtil.encrypt(inputFilePath, "");
			// 修改文件状态

			Map<String, Object> updataMap = new HashMap<String, Object>();
			updataMap.put("uploadstate", FileUploadStateEnum.Splice);
			StringBufferProxy sql = new StringBufferProxy();
			sql.appendSingle(" 1=1 ");
			sql.appendSingle(" and identifier = '{0}' and sourceid='{1}' and sourcetype='{2}'", identifier, sourceid,
					sourcetype);
			fileManageService.updateByWhere(updataMap, sql.toString());
			log.info("加密文件：" + inputFilePath + " 完成");
			dataProcessChain.doProcess(paraMap);
		} catch (Exception e) {
			throw new ServiceException(e.getMessage());
		}
		return false;
	}

}
